package com.ebt.m.proposal_v2.dao;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseIView;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.customer.net.constant.HttpConfig;
import com.ebt.m.customer.net.http.PolicyHttps;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.proposal_v2.base.Constant;
import com.ebt.m.proposal_v2.dao.request.DraftParam;
import com.ebt.m.proposal_v2.dao.request.ParamBrandProducts;
import com.ebt.m.proposal_v2.dao.request.ParamGetAttaches;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.request.ParamSaveProposalTaps;
import com.ebt.m.proposal_v2.dao.request.ProposalParam;
import com.ebt.m.proposal_v2.dao.response.ResponseAttach;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.dao.response.ResponseBrandProduct;
import com.ebt.m.proposal_v2.dao.response.ResponseDraft;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.dao.response.ResponseMainDetail;
import com.ebt.m.proposal_v2.dao.response.ResponseMakeProposal;
import com.ebt.m.proposal_v2.dao.response.ResponsePickNum;
import com.ebt.m.proposal_v2.dao.response.ResponseProductProperties;
import com.ebt.m.proposal_v2.dao.response.ResponseProposalTap;
import com.ebt.m.proposal_v2.dao.response.ResponseSaveTaps;
import com.ebt.m.proposal_v2.dao.response.ResponseSendProposal;
import com.ebt.m.proposal_v2.dao.response.ResponseTapDelete;
import com.ebt.m.proposal_v2.dao.response.ResponseTapFile;
import com.ebt.m.proposal_v2.retrofit.ErrorGenerator;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.RetrofitSubscriber;
import com.ebt.m.utils.UIHelper;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;

/**
 * 计划书请求
 *
 * @author archer.qi
 * Created on 2017/5/12.
 */
public class ProposalApi {
    private Context mContext;
    private EBTAPI mProposalApi;
    private static ProposalApi instance;

    public static ProposalApi getInstance(Context context) {
        if (instance == null) {
            synchronized (PolicyHttps.class) {
                if (instance == null) {
                    instance = new ProposalApi(context);
                }
            }
        }
        return instance;
    }

    private ProposalApi(Context context) {
        mContext = context;
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(HttpConfig.DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        mProposalApi = AppInitService.getEbtApi();
    }

    /**
     * 检查网络
     *
     * @param callback
     * @return
     */
    private boolean checkNetwork(OnResponseListener callback) {
        boolean isNetworkConnected = NetUtils.isNetworkConnected(mContext);
        if (!isNetworkConnected) {
            UIHelper.makeToast(mContext, mContext.getString(R.string.network_fail));
            if (callback != null) {
                callback.onFailure(ErrorGenerator.networkError());
            }
            return false;
        }
        return true;
    }

    /******************************************************
     * 计划书制作接口
     ******************************************************/

    /**
     * 保存为计划书草稿
     *
     * @param proposal
     * @param callback
     */
    public void saveProposalToDraft(DraftParam proposal, OnResponseListener<ResponsePickNum> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.saveToProposalDraft(proposal).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 制作计划书
     *
     * @param proposal
     * @param callback
     */
    public void makeProposal(ProposalParam proposal, OnResponseListener<ResponseMakeProposal> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.makeProposal(proposal).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取计划书草稿
     *
     * @param proposalId
     * @param callback
     */
    public void getProposalDraft(String proposalId, OnResponseListener<ResponseDraft> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.getProposalDraft(proposalId).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 校验计划书
     *
     * @param proposal
     * @param callback
     */
    public void verifyProposal(ProposalParam proposal, OnResponseListener<List<String>> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.verifyProposal(proposal).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /******************************************************
     * 计划书预览接口
     ******************************************************/

    /**
     * 发送计划书
     *
     * @param proposalID
     */
    public void sendProposal(String proposalID, OnResponseListener<ResponseSendProposal> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.sendPropsosal(proposalID).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 保存计划书语音
     *
     * @param params
     */
    public void saveProposalTaps(ParamSaveProposalTaps params, OnResponseListener<ResponseSaveTaps> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.saveProposalTaps(params).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 上传计划书语音
     *
     * @param filePart 语音文件
     * @param callback
     */
    public void updateProposalTap(MultipartBody.Part filePart, OnResponseListener<ResponseTapFile> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.updateProposalTapToOSS(filePart, "", Constant.PROPOSAL_TAP_UPLOAD_OSS_BUCKETNAME, Constant.PROPOSAL_TAP_UPLOAD_OSS_PATH)
                    .compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 删除计划书语音
     *
     * @param tapId
     */
    public void deleteProposalTap(String tapId, OnResponseListener<ResponseTapDelete> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.deleteProposalTap(tapId)
                    .compose(RxUtil.rxTransformer(baseIView))
                    .subscribe(new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 根据计划书ID获取语音信息
     *
     * @param proposalId
     */
    public void getProposalTapById(String proposalId, OnResponseListener<List<ResponseProposalTap>> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.getProposalTapById(proposalId)
                    .compose(RxUtil.rxTransformer(baseIView))
                    .subscribe(new RetrofitSubscriber<>(callback));
        }
    }


    /******************************************************
     *  计划书主险附加险接口
     ******************************************************/

    /**
     * 获取附加险列表
     *
     * @param param
     * @param callback
     */
    public void getAdditionalProducts(ParamGetAttaches param, OnResponseListener<List<ResponseAttach>> callback, BaseIView baseIView) {
        if (param == null) {
            throw new NullPointerException("getAdditionalProducts param is null");
        }
        if (checkNetwork(callback)) {
            mProposalApi.getAdditionalRisks(param)
                    .compose(RxUtil.rxTransformer(baseIView))
                    .subscribe(new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取常用产品列表
     *
     * @param param
     * @param callback
     */
    public void getMainProducts(ParamGetRisks param, OnResponseListener<List<ResponseMain>> callback, BaseIView baseIView) {
        if (param == null) {
            throw new NullPointerException("getMainProducts param is null");
        }
        if (checkNetwork(callback)) {
            mProposalApi.getCommonRisks(param.createQueryMap())
                    .compose(RxUtil.rxTransformer(baseIView))
                    .subscribe(new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取产品详情｛保险选项, 绑定附加险｝
     *
     * @param productId
     * @param callback
     */
    public void getMainDetails(int productId, String age, int sex, int profession, OnResponseListener<ResponseMainDetail> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
//            RxUtils.compose(mProposalApi.getMainDetails(productId, age, sex, profession)).subscribe(
//                    new RetrofitSubscriber<>(callback));
            mProposalApi.getMainDetails(productId, age, sex, profession).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取默认产品详情｛保险选项, 绑定附加险, 投保人默认信息｝
     *
     * @param productId
     * @param callback
     */
    public void getDetailMainProductDetails(int productId, OnResponseListener<ResponseMainDetail> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
//            RxUtils.compose(mProposalApi.getDefaultMainProductDetails(productId)).subscribe(
//                    new RetrofitSubscriber<>(callback));
            mProposalApi.getDefaultMainProductDetails(productId).compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取品牌列表
     *
     * @param callback
     */
    public void getBrands(OnResponseListener<List<ResponseBrand>> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.getBrands().compose(RxUtil.rxTransformer(baseIView)).subscribe(
                    new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取品牌关联的产品并提交到历史记录
     *
     * @param callback
     */
    public void getBrandProductsAndSubmitHistory(ParamBrandProducts param, OnResponseListener<List<ResponseBrandProduct>> callback, BaseIView baseIView) {
        if (param == null) {
            throw new NullPointerException("getBrandProductsAndSubmitHistory param is null");
        }
        if (checkNetwork(callback)) {
            mProposalApi.getBrandProductsAndSubmitHistory(param.createParamMap()).compose(RxUtil.rxTransformer(baseIView))
                    .subscribe(new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 搜索品牌产品 - 主险搜索
     *
     * @param callback
     */
    public void searchBrandProducts(ParamBrandProducts param, OnResponseListener<List<ResponseBrandProduct>> callback, BaseIView baseIView) {
        if (param == null) {
            throw new NullPointerException("searchBrandProducts param is null");
        }
        if (checkNetwork(callback)) {
            mProposalApi.searchBrandProducts(param.companyId, param.createSearchParamMap())
                    .compose(RxUtil.rxTransformer(baseIView)).subscribe(new RetrofitSubscriber<>(callback));
        }
    }

    /**
     * 获取产品属性
     *
     * @param productId
     * @param callback
     */
    public void getProductProperties(int productId, OnResponseListener<List<ResponseProductProperties>> callback, BaseIView baseIView) {
        if (checkNetwork(callback)) {
            mProposalApi.getProductPropertiesById(productId)
                    .compose(RxUtil.rxTransformer(baseIView)).subscribe(new RetrofitSubscriber<>(callback));
        }
    }

}
